#include<stdio.h>
#include<stdlib.h>
typedef struct STU {
	long  number;
	float score;
}STU;
void Menu()
{
	printf("Management for Students' scores\n");
	printf("1.Input record\n");
	printf("2.Caculate total and average score of course\n");
	printf("3.Sort in descending order by score\n");
	printf("4.Sort in ascending order by number\n");
	printf("5.Search by number\n");
	printf("6.Statistic analysis\n");
	printf("7.List record\n");
	printf("0.Exit\n");
	printf("Please Input your choice:\n");
}
void fun1(STU* a, int n)
{
	printf("Input student's ID, name and score:\n");
	for (int i = 0;i < n;i++)
	{
		scanf("%ld%f", &a[i].number, &a[i].score);
	}
}
void fun2(STU* a, int n)
{
	float sum = 0;
	for (int i = 0;i < n;i++)
	{
		sum += a[i].score;
	}
	printf("sum=%.0f,aver=%.2f\n", sum, sum / n);
}

int Compare1(const void* a, const void* b)
{
	return (int)(((STU*)b)->score - ((STU*)a)->score);
}

void fun3(STU* a, int n)
{
	qsort(a, n, sizeof(STU), Compare1);
	printf("Sort in descending order by score:\n");
	for (int i = 0;i < n;i++)
	{
		printf("%ld\t%.0f\n", a[i].number, a[i].score);
	}
}
int Compare3(const void* a, const void* b);
void fun4(STU* a, int n)
{
	qsort(a, n, sizeof(STU), Compare3);
	printf("Sort in ascending order by number:\n");
	for (int i = 0;i < n;i++)
	{
		printf("%ld\t%.0f\n", a[i].number, a[i].score);
	}
}
void fun5(STU* a, int n)
{
	printf("Input the number you want to search:\n");
	long tmp;
	scanf("%ld", &tmp);
	for (int i = 0;i < n;i++)
	{
		if (a[i].number == tmp)
		{
			printf("%ld\t%.0f\n", a[i].number, a[i].score);
			return;
		}
	}
	printf("Not found!\n");

}
void fun6(STU* a, int n)
{
	int arr[6] = { 0 };
	for (int i = 0;i < n;i++)
	{
		if (a[i].score < 60)
			arr[0]++;
		else if (a[i].score <= 69)
			arr[1]++;
		else if (a[i].score <= 79)
			arr[2]++;
		else if (a[i].score <= 89)
			arr[3]++;
		else if (a[i].score <= 99)
			arr[4]++;
		else
			arr[5]++;
	}
	printf("<60\t%d\t%.2f%%\n", arr[0], 100.0 * arr[0] / n);
	printf("%d-%d\t%d\t%.2f%%\n", 60, 69, arr[1], 100.0 * arr[1] / n);
	printf("%d-%d\t%d\t%.2f%%\n", 70, 79, arr[1], 100.0 * arr[2] / n);
	printf("%d-%d\t%d\t%.2f%%\n", 80, 89, arr[1], 100.0 * arr[3] / n);
	printf("%d-%d\t%d\t%.2f%%\n", 90, 99, arr[1], 100.0 * arr[4] / n);
	printf("%d\t%d\t%.2f%%\n", 100, arr[5], 100.0 * arr[5] / n);
}
int Compare3(const void* a, const void* b)
{
	return (int)(((STU*)a)->number - ((STU*)b)->number);
}
void fun7(STU* a, int n)
{
	qsort(a, n, sizeof(STU), Compare3);

	for (int i = 0;i < n;i++)
	{
		printf("%ld\t%.0f\n", a[i].number, a[i].score);
	}
}
int main()
{
	printf("Input student number(n<30):\n");
	int n;
	scanf("%d", &n);
	STU a[30];
	int input;
	do {

		Menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			fun1(a, n);
			break;
		case 2:
			fun2(a, n);
			break;
		case 3:
			fun3(a, n);
			break;
		case 4:
			fun4(a, n);
			break;
		case 5:
			fun5(a, n);
			break;
		case 6:
			fun6(a, n);
			break;
		case 7:
			fun7(a, n);
			break;
		case 0:
			printf("End of program!\n");
			break;
		default:
			printf("Input error!\n");

		}

	} while (input);
	return 0;
}